define([
    'text!./logonWidget.html',
    '../../service.js',
	"../../assets/vendor/oniui/dialog/avalon.dialog"
], function(sourceHtml, api) {
    var widget = avalon.ui.logonWidget = function(element, data, vmodels) {
        var vmodel = avalon.define(data.logonWidgetId, function(vm) {

        	vm.logon = '';
        	vm.uri = '';
            vm.showDialog = function(id) {
                document.getElementById("iframe").setAttribute("src", "http://pt.ava.com.cn/authorize.action?extra=" + location.origin + "/open/qq/receive.html");
                avalon.vmodels[id].toggle = true;
            }
            vm.showAvaDialog = function(id) {
                avalon.vmodels[id].toggle = true;
            }
            vm.avalogon = function(id) {
                $.ajax({
                    url: 'http://xy.ava.com.cn/lxx/j_spring_security_check',
                    data: {
                        j_username: $('[name="ava_name"]').val(),
                        j_password: $('[name="ava_password"]').val(),
                        isAppLogon: 1,
                        deviceType: 5
                    },
                    type: 'post',
                    success: function(res) {
                        if (res.status == 0) {
                            vm.uri = 'AVA:' + (res.result && res.result.jsessionid);
                            vm.logon = 'avaLogon';
                            avalon.vmodels[id].toggle = false;
                        }
                    },
                    error: function() {
                        alert("出错！")
                    }
                })
            }

            vm.dialogOpts = {
                title: 'QQ-登记',
                height: 500,
                width: 900,
                container:'logonWidget'

            }
            vm.avaDialogOpts = {
                title: 'AVA-登记',
                height: 500,
                width: 600,
                container:'logonWidget'

            }
            window.setValue = function(uri){
		        vm.uri = uri;
		        vm.logon = 'QQLogon';
		        avalon.vmodels['dialog'].toggle = false
		    }
            vm.$init = function() {
            	var tmp = sourceHtml;
            	// tmp = tmp.replace(/{{avadialog}}/g, ('qqdialog'+(new Date()-0)));
            	// tmp = tmp.replace(/{{qqdialog}}/g, ('avadialog'+(new Date()-0)));
                element.innerHTML = tmp;
                avalon.scan(element, [vmodel].concat(vmodels))
            }
            vm.$remove = function() {
                element.innerHTML = element.textContent = "";
            }
        })
        return vmodel;
    }
    return avalon;
})
